package dailearn.consumer.controller;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author daify
 * @date 2019-07-03 10:55
 **/
@RestController
@Log
public class ConsumerController {

    @Autowired LoadBalancerClient loadBalancerClient;
    @Autowired RestTemplate restTemplate;

    @RequestMapping(value = "/consumerGetService",method = RequestMethod.GET)
    public String getService() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("base-producer");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getService";
        log.info(url);
        return restTemplate.postForObject(url,null, String.class);
    }
}
